
$(function () {
    /*抽取常用组件*/
    var claims_list = $("#claims_list");
    var claims_dialog = $("#claims_dialog");
    var claims_form = $("#claims_form");

    var object = {
        add: function () {
            claims_dialog.dialog('setTitle', '新增案件');
            claims_form.form('clear');
            claims_dialog.dialog('open');
        },
        edit: function () {
            var row = claims_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                claims_form.form('clear');
                claims_form.form('load', row);
                claims_dialog.dialog('setTitle', '编辑案件');
                claims_dialog.dialog('open');
            }
        },
        save: function () {
            claims_form.form("submit", {
                    url: "/claims/saveOrUpdate.do",
                    success: function (data) {
                        var data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert('温馨提示', data.msg, 'info', function () {
                                claims_dialog.dialog('close');
                                claims_list.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    }
                }
            );
        },
        cancel: function () {
            claims_dialog.dialog('close');
        }
        ,
        reload: function () {
            claims_list.datagrid('reload');
        },

        //查询数据库,验证保单有效性:
        receiptsVerify: function () {
            var row = claims_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                return;
            }
            else{
                $.get('/claims/receiptsVerify.do', {id: row.id, insuranceNum: row.insuranceNum},
                    function (data) {
                    if (data.success) {
                        $.messager.alert('温馨提示', "保单验证完成...!", 'info', function () {
                            claims_list.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', "保单验证失败,请再尝试...", 'error');
                    }
                })
            }
        },
        searchInfo: function () {
            var keyword = $("#keyword").textbox('getValue');
            var minTime = $("#minTime").datebox('getValue');
            var maxTime = $("#maxTime").datebox('getValue');

            claims_list.datagrid("load", {
                keyword: keyword,
                minTime: minTime,
                maxTime: maxTime
            });
        },
        //选中受理,新增一条事故受理信息
        accept: function(){
            var row = claims_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                return;
            }
            $.get('/claims/accept.do', {id: row.id}, function(data){
                if(data.success){
                    $.messager.alert('温馨提示', "受理成功!", 'info', function () {
                        claims_list.datagrid('reload');
                    });
                }else {
                    $.messager.alert('温馨提示', data.msg, 'info');
                }
            })
        }

    }
    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    });

    claims_list.datagrid({
        fit: true,
        url: "/claims/query.do",
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#claims_toolbar',
        columns: [[
            {field: 'state', title: '案件状态', width: 120, align: 'center',
                formatter: function (value, row, index) {
                    switch (value) {
                        case 0:
                            return  '<font style="color:red;">保单不存在</font>';
                            break;
                        case 1:
                            return '<font style="color:#050680;">保单待验证</font>';
                            break;
                        case 2:
                            return '<font style="color:red;">保单过期失效</font>';
                            break;
                        case 3:
                            return '<font style="color:#1d9c30;">保单有效</font>';
                            break;
                        default:
                            break;
                    }
                }
               },
            {field: 'reporterName', title: '报案人姓名', width: 100, align: 'center'},
            {field: 'gender', title: '性别', width: 100, align: 'center',
                formatter: function (value, row, index) {
                    if (row.gender==0){
                        return "女"
                    }else{
                        return "男"
                    }
                }
            },
            {field: 'telNumber', title: '联系电话', width: 100, align: 'center'},
            {field: 'accidentTime', title: '事发时间', width: 100, align: 'center'},
            {field: 'accidentAddress', title: '事发地点', width: 100, align: 'center'},
            {field: 'accidentDescription', title: '案件描述', width: 100, align: 'center'},
            {field: 'insuranceNum', title: '保单编号', width: 100, align: 'center'},
            {field: 'carNumber', title: '车牌号', width: 100, align: 'center'},
            {field: 'remark', title: '备注', width: 100, align: 'center'}


        ]]
    })

    claims_dialog.dialog({
        closed: true,
        width: 550,
        height: 450,
        buttons: $("#claims_diaButtons"),
    })


})
